package com.zf.museum.controller;



import com.zf.museum.bean.Question;
import com.zf.museum.bean.Result;
import com.zf.museum.model.User;

import com.zf.museum.service.UserService;
import com.zf.museum.utils.OpenIdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Set;


@Api(description = "微信用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private OpenIdUtil openIdUtil;

    @ApiOperation(value = "保存微信用户openId和昵称")
    @PostMapping("/login")
    public Result login(@RequestParam("name") String name, @RequestParam("openId") String openId, HttpSession session) {

        Result<String> result = userService.login(name, openId);
        result.setData(session.getId());
        return userService.login(name, openId);
    }

    @ApiOperation(value = "修改昵称", notes = "根据前端传来的openId，修改昵称")
    @ApiImplicitParam(name = "name", value = "修改的昵称", required = true, paramType = "query", dataType = "string")
    @PostMapping("/updateName")
    public Result updateName(@RequestParam("name") String name, @RequestParam("openId") String openId) {

        return userService.updateName(name, openId);
    }

    @PostMapping("/getOpenId")
    public String getOpenId(@RequestParam("code") String code) {

        return openIdUtil.getOpenid(code);
    }

    @GetMapping("/getUserByOpenId")
    public User getUserByOpenId(@RequestParam("openId") String openId) {
        return userService.getUserByOpenId(openId);
    }

    @GetMapping("/getTop10User")
    public Set<User> getTop10User(@RequestParam("openId") String openId) {

        return userService.getTop10User(openId);
    }

    /**
     * 判断题目的对错
     * @param question
     * @return
     */
    @PostMapping("/judge")
    public Result judge(@RequestBody Question question) {

        return userService.judge(question);
    }

}
